﻿using System;
using System.Web.UI.WebControls;
using br.com.SistemaChamados.Controller;
using br.com.SistemaChamados.Enum;
using br.com.SistemaChamados.Model;
using br.com.SistemaChamados.Constants;
using br.com.SistemaChamados.Controller.Common;
using br.com.SistemaChamados.Util;

public partial class Pages_CallSubCategoryMain : System.Web.UI.Page
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        ViewStateUserKey = Session.SessionID;
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        br.com.SistemaChamados.Model.Common.User loggedUser = new br.com.SistemaChamados.Model.Common.User();
        UserController userController = new UserController();
        if (Session[UserConstants.SESSION_USER_LOGGED_IN] != null)
        {
            loggedUser.Id = Convert.ToInt32(Session[UserConstants.SESSION_USER_LOGGED_IN]);
            loggedUser = userController.GetUser(loggedUser);
            this.MasterPageFile = MasterPageDefiner.defineMasterPageByAccessRole(loggedUser.Profile);
            ((Label)Master.FindControl("lblUser")).Text = loggedUser.Name;
            this.Title = "SysCall - Sistema de chamados [logado como " + loggedUser.Name + "]";
        }
        else
        {
            Response.Redirect("~/Pages/Login/Login.aspx");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        CallSubCategoryController callSubCategoryController = new CallSubCategoryController();
        CallSubCategory callSubCategory = new CallSubCategory();
        try
        {
            int id = Convert.ToInt32(e.CommandArgument);
            switch (e.CommandName)
            {
                case "ExcludeData":
                    callSubCategory.Id = id;
                    if (callSubCategoryController.DeleteCallSubCategory(callSubCategory) == EnumGenericState.GENERIC_STATE_SUCCESS)
                    {
                        Response.Write(@"<script language='javascript'>alert('Subcategoria excluída com sucesso!');</script>");
                        Response.Redirect("CallSubCategoryMain.aspx");
                    }
                    else
                    {
                        Response.Write(@"<script language='javascript'>alert('Ocorreu um erro durante a exclusão, por favor tente novamente!');</script>");
                    }
                    break;

                case "UpdateData":
                    Session[CallSubCategoryConstants.SESSION_SUBCALLCATEGORY_ID] = id;
                    Response.Redirect("CallSubCategoryUpdate.aspx");
                    break;
            }
        }
        catch (Exception ex)
        {
            Response.Write(@"<script language='javascript'>alert('Ocorreu um erro ao deletar a subcategoria: <br>" + ex.Message + ".');</script>");
            callSubCategory = null;
            callSubCategoryController = null;
        }
    }
}